<?php
namespace app\common\behavior;

use think\Config;

/**
 * 错误编号：
 *       0000代表成功
 *       0***代表请求错误
 *       8***代表芯易出现错误
 * @author xy
 *
 */
class ApiStart {
	public function run($params) {
		return true;
		//处理session
		if(isset($_SERVER['HTTP_XY_SOFT_TYPE']) && $_SERVER['HTTP_XY_SOFT_TYPE'] == 'wesoft'){
			if(isset($_SERVER['HTTP_X_WX_SESSIONKEY']) && !empty($_SERVER['HTTP_X_WX_SESSIONKEY'])){
				$sessionconfig = config('session');
				if(isset($_SESSION)) session_destroy();
				$sessionconfig['id'] = $_SERVER['HTTP_X_WX_SESSIONKEY'];
				$sessionconfig['var_session_id'] = $_SERVER['HTTP_X_WX_SESSIONKEY'];
				$sessionconfig['auto_start'] = true;
				session($sessionconfig);
			}
		}else if(!empty(input('get.ssid'))){
			$sessionconfig = config('session');
			if(isset($_SESSION)) session_destroy();
			$sessionconfig['id'] = input('get.ssid');
			$sessionconfig['var_session_id'] = input('get.ssid');
			$sessionconfig['auto_start'] = true;
			session($sessionconfig);
		}
		Config::load(CONF_PATH.'system/config.php');
		Config::load(APP_PATH.'api/config/config.php');
		
		//获取参数 //只获取POST的数据
		$data = $tmpdata = input('post.');
		//定义所有必传公共必传参数
		$paramsData = config('apipublicparams');
		foreach ($paramsData as $v){
			//删除参数方便后面计算是否存在多余参数
			unset($tmpdata[$v['field']]);
			//验证必传参数
			if($v['required']){
				if(!isset($data[$v['field']])){
					apiMsg($v['title'].$v['field'].'为必传公共参数！','0001');
				}
				if(empty($data[$v['field']])){
					apiMsg('必传公共参数'.$v['title'].$v['field'].'不能为空！','0001');
				}
			}
			//
			if(isset($data[$v['field']])){
				$funcname = 'is'.$v['type'];
				if(!method_exists($this,$funcname)){
					apiMsg('公共参数'.$v['title'].$v['field'].'验证规则配置错误，请通知平台对接人！','8001');
				}
				if(!$this->$funcname($data[$v['field']],$v)){
					apiMsg('公共参数'.$v['title'].$v['field'].'不符合要求，请参考文档说明进行修改！','0001');
				}
			}
		}
		
		$mothed = explode('.', $data['mothed']);//解析请求的API方法
		$class = 'app\api\model\\'.$mothed[0].'\\'.$mothed[1];
		if(!class_exists($class)){
			apiMsg('请求的接口不存在！','0002');
		}
		$newMothed = new $class;
		if(!method_exists($newMothed,$mothed[2])){
			apiMsg('请求的接口不存在！','0003');
		}
		//读取接口参数配置文件
		$filename = APP_PATH.'api/config/'.$mothed[0].'/'.$mothed[1].'/'.$mothed[2].'.php';
		if(!file_exists($filename)){
			apiMsg('接口配置文件缺失','8001');
		}
		Config::load($filename);
		$paramsData = config('apiparams');
		
		foreach ($paramsData as $v){
			//删除参数方便后面计算是否存在多余参数
			unset($tmpdata[$v['field']]);
			//验证必传参数
			if($v['required']){
				if(!isset($data[$v['field']])){
					apiMsg($v['title'].$v['field'].'为必传参数！','0001');
				}
				if(empty($data[$v['field']])){
					apiMsg('接口必传参数'.$v['title'].$v['field'].'不能为空！','0001');
				}
			}
			//
			if(isset($data[$v['field']])){
				$funcname = 'is'.$v['type'];
				if(!method_exists($this,$funcname)){
					apiMsg('接口参数'.$v['title'].$v['field'].'验证规则配置错误，请通知平台对接人！','8001');
				}
				if(!$this->$funcname($data[$v['field']],$v)){
					apiMsg('接口参数'.$v['title'].$v['field'].'不符合要求，请参考文档说明进行修改！','0001');
				}
			}
		}
		if(count($tmpdata) > 0){
			$fields = [];
			foreach ($tmpdata as $key=>$vtmp){
				$fields[] = $key;
			}
			apiMsg('参数'.implode(',', $fields).'为多余参数，请删除！','0001');
		}
		
		//获取APPID及权限信息判断是否允许调用这个方法
		$appid = $data['appid'];
		$application = Application::get(['appid'=>$appid]);
		if(empty($application)){
			apiMsg('应用不存在，请检查APPID是否配置正确！','0004');
		}
		if($application->status != '1'){
			apiMsg('应用状态不可用，如有疑问可联系平台合作对接人！','0005');
		}
		//校验签名
		$sign = getApiSign($data, $application->appsecret);
		
		if($sign != $data['sign']){
			apiMsg('签名错误！'.$sign,'0006');
		}
		
		$func = $mothed[2];
		echo json_encode($newMothed->$func());
	}
	/**
	 * 验证string
	 */
	public function isstring($var,$vlaid){
	    return is_string($var);
	}
	/**
	 * 验证number
	 */
	public function isnumber($var,$vlaid){
	    return is_numeric($var);
	}
	/**
	 * 验证url
	 */
	public function isurl($var,$vlaid){
	    return preg_match("/^((http|https|ftp|telnet|news):\/\/)?([a-z0-9_\-\/\.]+\.[][a-z0-9:;&#@=_~%\?\/\.\,\+\-]+)$/", $var);
	}
	/**
	 * 验证email
	 */
	public function isemail($var,$vlaid){
	    return preg_match("/^([a-z0-9_\-]+)@([a-z0-9_\-]+\.[a-z0-9\-\._\-]+)$/", $var);
	}
	/**
	 * 验证手机号
	 * @param number $var
	 * @param unknown $vlaid
	 * @return number
	 */
	public function isphone($var,$vlaid){
	    return preg_match("/^1[34578]\d{9}$/", $var);
	}
	/**
	 * 验证正则
	 */
	public function isregular($var,$vlaid){
	    return isset($vlaid['regular']) && preg_match($vlaid['regular'], $var);
	}
	
}
?>